package com.scisoft.scan.control;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;

public class NumberInputActivity extends Activity {

    private Button bt_all;
    private Button bt_ok;
    private Button bt_cancel;
    private EditText et_data;
    private int minNum = 0;
    private int maxNum = 0;
    private int rowNum = 0;
    private int result = 0;
    private String supplierCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_number_input);
        //参数传递
        Intent intent = getIntent();
        minNum = intent.getIntExtra("minNum",0);
        maxNum = intent.getIntExtra("maxNum",0);
        rowNum = intent.getIntExtra("rowNum",0);
        supplierCode = intent.getStringExtra("supplierCode");

        //初始化
        et_data =findViewById(R.id.et_numberinput_data);
        bt_all = findViewById(R.id.bt_numberinput_all);
        bt_ok = findViewById(R.id.bt_numberinput_ok);
        bt_cancel = findViewById(R.id.bt_numberinput_cancel);
        bt_all.setOnClickListener(onClickListener);
        bt_ok.setOnClickListener(onClickListener);
        bt_cancel.setOnClickListener(onClickListener);

        et_data.setText("");
    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (v.getId()) {
                case R.id.bt_numberinput_all:
                    result = maxNum;
                    intent.putExtra("result", result);
                    intent.putExtra("rowNum", rowNum);
                    intent.putExtra("supplierCode",supplierCode);
                    NumberInputActivity.this.setResult(RESULT_OK, intent);
                    NumberInputActivity.this.finish();
                    break;
                case R.id.bt_numberinput_ok:
                    String temp  = et_data.getText().toString();
                    if (temp.equals("")) {
                        Toast.makeText(getApplicationContext(), "数量不能为空！", Toast.LENGTH_LONG).show();
                        return;
                    }
                    else if (!CommonUtil.isNumeric(temp))
                    {
                        Toast.makeText(getApplicationContext(), "请输入数字！", Toast.LENGTH_LONG).show();
                        return;
                    }
                    else
                    {
                        result = Integer.parseInt(temp);
                        if(result <= minNum || result > maxNum) {
                            Toast.makeText(getApplicationContext(), "数量范围大于" + minNum + "小于"+maxNum, Toast.LENGTH_LONG).show();
                            return;
                        }

                    }
                    intent.putExtra("result", result);
                    intent.putExtra("rowNum", rowNum);
                    intent.putExtra("supplierCode",supplierCode);
                    NumberInputActivity.this.setResult(RESULT_OK, intent);
                    NumberInputActivity.this.finish();
                    break;
                case R.id.bt_numberinput_cancel:
                    NumberInputActivity.this.setResult(RESULT_CANCELED, intent);
                    NumberInputActivity.this.finish();
                    break;
            }
            InputMethodManager imm = (InputMethodManager) NumberInputActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm!=null)
                imm.hideSoftInputFromWindow(et_data.getWindowToken(), 0);

        }
    };


}
